import Singleton from "../../framework/bo/Singleton";
import StorageMgr from "../../framework/manager/storageManager";
import BaseSocket from "../../framework/net/BaseSocket";
import SocketManager from "../../framework/net/SocketManager";
import Player from "../vo/Player";
import { AreaInfo, AreaVo, RoleVo, ServerInfo, ServerVo, UserInfo } from "../vo/GOBERTS";
import { Constant } from "../../Enum/Enum";
import { ClientEvent } from "../../framework/bo/ClientEvent";
import { UIManager } from "../../framework/manager/uiManager";
import { proto } from "../../../../tools/proto/protos";
/**
 * 游戏的所有运行数据存放处，包括初始化从本地内存获取的玩家数据
 * 1、在区服信息获取到之后就出发分发事件设置了选区结果
 * 2、设置用户信息每次都会更新本地内存的用户信息
 * 3、存放玩家角色信息
 */
export default class ClientContext extends Singleton {
    platformId: string;// 玩家账号playerId
    userInfo: UserInfo;// 连上登陆服务器之后需要更新到最新状态 TODO

    playerVo: Player = null;// playerVo
    roleInfo: proto.RoleInfoProto = null;// 待开发todo

    hallSocket: BaseSocket = null;// 大厅服务器连接
    hallSocketManager: SocketManager = null;
    serverVo: ServerVo;// 玩家所选登录服务器
    areaVo: AreaVo;//玩家选登陆区信息
    private serverInfos: ServerInfo[];// 游戏区服信息

    public static get Instance(): ClientContext {
        return super.GetInstance<ClientContext>();
    }
    clear() {
        this.platformId = null;
        this.userInfo = null
        this.playerVo = null;// playerVo
        this.roleInfo = null;// 待开发todo
        this.hallSocket = null;// 大厅服务器连接
        this.hallSocketManager = null;
        this.serverVo;// 玩家所选登录服务器
        this.areaVo;//玩家选登陆区信息
        this.serverInfos = null;// 游戏区服信息
    }

    public init() {
        ClientEvent.on(Constant.EVENT_NAME.on_init_server_info, this.setServerVoAndAreaVo, this);
        return new Promise<void>((resolve, reject) => {
            console.log("初始化Context");
            // this.platformId = StorageManager.instance.getGlobalData(Constant.LOCAL_CACHE.PlatformId);
            this.userInfo = StorageMgr.Ins.getGlobalData(Constant.LOCAL_CACHE.UserInfo);
            if (this.userInfo == null) {
                console.log("玩家没有登陆数据");
            } else {
                this.platformId = this.userInfo.platformId;
            }
            resolve();
        });
    }

    public clearnUserInfo(cb?: Function) {// 清除玩家本地数据
        console.log("清除玩家本地数据");
        this.userInfo = null;
        this.platformId = null;
        StorageMgr.Ins.setGlobalData(Constant.LOCAL_CACHE.UserInfo, null);
        cb && cb();
    }

    /**
     * @param ext 是否执行
     * @returns 进入游戏检查
     */
    public checkEnterGame(ext: boolean): number | string {
        if (!this.hallSocketManager.isConnected()) {
            console.log("没有连接大厅服务器");
            return "没有连接大厅服务器";
        }
        if (this.serverInfos == null || this.serverInfos.length == 0) {
            console.log("没有区服信息");
            return "没有区服信息";
        }
        if (this.userInfo == null
            || this.platformId == null
            || this.areaVo == null) {
            console.log("玩家没有登陆数据");
            if (ext) {
                UIManager.instance.showDialog(Constant.PANEL_NAME.LOGIN_PANEL);
            }
            return "玩家没有登陆数据";
        }
        // 是否自愿同意玩游戏
        return 0;
    }

    public set ServerInfos(value: ServerInfo[]) {
        this.serverInfos = value;
        console.log("更新区服信息", ClientContext.Instance.ServerInfos);
        ClientEvent.dispatchEvent(Constant.EVENT_NAME.on_init_server_info);
    }

    public get ServerInfos(): ServerInfo[] {
        return this.serverInfos;
    }

    public setServerVoAndAreaVo() {
        let area: AreaVo = {
            areaId: this.serverInfos[0].areaInfos[0].areaId
        }
        this.areaVo = area;

        let areaVos: AreaVo[] = [area];
        let server: ServerVo = {
            selectedServerId:
                this.serverInfos[0].serverId,
            areaVos: areaVos
        }
        this.serverVo = server;
    }
    //判断客户端是否有用户信息
    public hasUserInfo(): boolean {
        return this.userInfo != null;
    }

    //判断客户端是否有区服信息
    public hasServerInfo(): boolean {
        return this.serverInfos != null && this.serverInfos.length > 0;
    }

    //判断客户端是否有平台信息
    public hasPlatformInfo(): boolean {
        return this.platformId != null;
    }

    //判断客户端是否有区服信息
    public hasAreaInfo(): boolean {
        return this.areaVo != null;
    }

    //更改本地客户端用户信息userInfo
    public set UserInfo(userInfoProto: proto.UserInfoProto) {
        let userInfo0: UserInfo = {
            account: userInfoProto.account,
            platformId: userInfoProto.platformId,
            password: userInfoProto.password,
            id: userInfoProto.id,
            registerTime: userInfoProto.registerTime,
            state: userInfoProto.state,
        };
        this.clearnUserInfo();
        this.platformId = userInfoProto.platformId;
        this.userInfo = userInfo0;
        console.log("玩家数据更新", this.userInfo);
        // 保存到本地内存
        StorageMgr.Ins.setGlobalData(Constant.LOCAL_CACHE.UserInfo, this.userInfo);
        // 用户信息更新事件分发 TODO
    }

    // public set RoleInfo(roleInfoProto: proto.RoleInfoProto) {
    //     this.roleInfo = roleInfoProto;
    // }
}

/*冰
* cen Restarg game. cocos game.restart 源码
* @zh 重新开始游戏
// */
// public restart (): Promise<void>{
//     const endFramePromise= new Promise<void>( (resolve): void =>{ director.once (Director.EVENT_END_FRAME, (): void => resolve)); });
//     return endFramePromise. then ((): void =>{
//     director.reset();
//     cclegacy. Object. _deferredDestroy ();
//     this.pause);
//     this. resume ();
//     this._shouldLoadLaunchScene= true;
//     SplashScreen.instance. curTime= 0:
//     this.
//     this.safeEmit( Game.EVENT RESTART);
// }
